#!/usr/bin/env bash

declare -a target_dir=("functional" "irgen")

tmpir=tmpirgen
tmpout=tmpout

for dir in "${target_dir[@]}"; do
  for file in test/"$dir"/*.sy; do
    basename=${file%.sy}
    if [ -f "$basename.in" ]; then
      echo "$file needs input. skipped."
      continue
    fi
    if [ ! -f "$basename.out" ]; then
      echo "$file has no .out, skipped."
      continue
    fi

    build/bin/sysyc "$file" >$tmpir
    lli "$tmpir" >$tmpout
    echo $? >>"$tmpout"

    if ! diff -Bw "$tmpout" "$basename.out" > /dev/null 2>&1 ; then
      echo "error on file $file"
      echo "ir generated: "
      cat $tmpir
      echo ""
      echo "diff result: "
      diff -Bw "$tmpout" "$basename.out"
      rm $tmpir $tmpout
      exit 1
    fi

    echo "$file finished."
  done
done

echo "all finished."
rm $tmpir $tmpout
exit 0
